package com.btshop.goods.service.impl;
import com.btshop.goods.mapper.ParaExtendMapper;
import com.btshop.goods.mapper.ParaMapper;
import com.btshop.goods.pojo.Para;
import com.btshop.goods.pojo.ParaExample;
import com.btshop.goods.service.ParaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import com.util.IdWorker;

/**
 * @ClassName: ParaServiceImpl
 * @description: Para业务层接口实现类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Service
public class ParaServiceImpl implements ParaService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ParaMapper paraMapper;
    @Autowired
    private ParaExtendMapper paraExtendMapper;


    /**
     * 根据商品分类id查询参数
     * @param categoryId
     * @return
     */
    @Override
    public List<Para> findByCategoryId(Long categoryId) {
        return paraExtendMapper.findParaByCategoryId(categoryId);
    }

    /**
     * Para条件+分页查询
     * @param para 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Para> findPage(Para para, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        ParaExample example = createExample(para);
        //执行搜索
        return new PageInfo<Para>(paraMapper.selectByExample(example));
    }

    /**
     * Para分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Para> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Para>(this.buildSelectAll());
    }

    /**
     * Para条件查询
     * @param para
     * @return
     */
    @Override
    public List<Para> findList(Para para){
        //构建查询条件
        ParaExample example = createExample(para);
        //根据构建的条件查询数据
        return paraMapper.selectByExample(example);
    }


    /**
     * Para构建查询对象
     * @param para
     * @return
     */
    public ParaExample createExample(Para para){
        ParaExample example=new ParaExample();
        ParaExample.Criteria criteria = example.createCriteria();
        if(para!=null){
            // 主键id
            if(!StringUtils.isEmpty(para.getId())){
                    criteria.andIdEqualTo(para.getId());
            }
            // 名称
            if(!StringUtils.isEmpty(para.getName())){
                    criteria.andNameLike("%"+para.getName()+"%");
            }
            // 选项
            if(!StringUtils.isEmpty(para.getOptions())){
                    criteria.andOptionsEqualTo(para.getOptions());
            }
            // 排序
            if(!StringUtils.isEmpty(para.getSeq())){
                    criteria.andSeqEqualTo(para.getSeq());
            }
            // 模板ID
            if(!StringUtils.isEmpty(para.getTemplateId())){
                    criteria.andTemplateIdEqualTo(para.getTemplateId());
            }
            // 创建时间
            if(!StringUtils.isEmpty(para.getCreateTime())){
                    criteria.andCreateTimeEqualTo(para.getCreateTime());
            }
            // 更新时间
            if(!StringUtils.isEmpty(para.getUpdateTime())){
                    criteria.andUpdateTimeEqualTo(para.getUpdateTime());
            }
            // 乐观锁版本号
            if(!StringUtils.isEmpty(para.getOptimistic())){
                    criteria.andOptimisticEqualTo(para.getOptimistic());
            }
            // 备注字段
            if(!StringUtils.isEmpty(para.getRemark())){
                    criteria.andRemarkEqualTo(para.getRemark());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delete(Long id){
        paraMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Para
     * @param para
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Para para){
        paraMapper.updateByPrimaryKey(para);
    }

    /**
     * 增加Para
     * @param para
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Para para){
        //设置主键值
        para.setId(idWorker.nextId());
        paraMapper.insert(para);
    }

    /**
     * 根据ID查询Para
     * @param id
     * @return
     */
    @Override
    public Para findById(Long id){
        return  paraMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Para全部数据
     * @return
     */
    @Override
    public List<Para> findAll() {
        return this.buildSelectAll();
    }

    public List<Para> buildSelectAll(){
        return paraMapper.selectByExample(null);
    }
}
